<?php
namespace backend\controllers;
use yii\web\Controller;
use backend\models\Article;
use common\Session;
use yii;
use yii\db\QueryBuilder;
use backend\models\Articlect;
/**
 * Created by PhpStorm.
 * User: 张
 * Date: 2015/9/22
 * Time: 22:56
 * 文章列表页面
 */
class ArticleController extends Controller{
    //public $enableCsrfValidation = false;
    public function init()
    {
        Session::getSession()==101 ? :$this->redirect(['/login']);
    }
    public function actionIndex(){
        $page= isset($_GET['page'])? $_GET['page'] :1;
        $limits=13;
        $data=$this->actionPage($page,$limits);
        $info=array(
            'rating'=>'文章列表',
            'define'=>'这里列出所有文章的基本信息。'
        );
        return $this->render('index',[
            'model' => $data['model'],
            /*分页需要的参数*/
            'data'=>$data,
            'info'=>$info,
        ]);
    }
    public function actionDelete(){
        $id=isset($_GET['id']) ?$_GET['id'] :'';
        $page=isset($_GET['page']) ?$_GET['page'] :'';
        $updateone=Article::find()->where(['id'=>$id])->all();
        $updateone[0]->delete();
        $this->redirect(['article/index','page'=>$page]);
    }
    public function actionUpdateall(){
        if(Yii::$app->request->isAjax){
            $data="(".implode(',',$_POST['data']).")";
            $status=$_POST['status'];
            $connection = Yii::$app->db;
            if($status==0){
                $connection->createCommand("DELETE FROM qiche_article WHERE id IN $data")->execute();
                return 'OK';
            }
            $connection->createCommand("UPDATE qiche_article SET status=$status WHERE id IN $data")->execute();
            return 'OK';
            //批量更新数据
        }
    }
    private function actionPage($pages,$limits){
        //判断page参数是否是负数或O等非法参数值
        if($pages<1){
            $page=1;
        }else{
            $page=$pages;
        }
        $count=Article::find()->count();
        if(!$count){
            return array(
                'page'=>1,//分几页
                'model'=>'',//当前分页的数据
                'backpage'=>1,//返回上一页
                'frontpage'=>1,//下一页
                'nowpage'=>1,//当前页
                'limits'=>$limits,//每页的数据条数
        );
        }
        $pag=ceil($count/$limits);
        $start=($page-1)*$limits;
        if($pages>$pag){
            $start=($pag-1)*$limits;
        }
        $sql="SELECT * FROM qiche_article  ORDER BY create_time DESC LIMIT $start,$limits";
        $models = Article::findBySql($sql)->asArray()->all();
        $category=Articlect::find()->asArray()->all();
        foreach($models as $k=>$v){
            //循环将文章分类转换成文字
            foreach($category as $kc=>$vc){
                if($vc['cid']==$v['category']){
                    $models[$k]['category']=$vc['name'];
                }

            }
        }
        if($page==1){
            $backpage=1;
        }else{
            $backpage=$page-1;
        }
        if($page==$pag){
            $frontpage=$pag;
        }else{
            $frontpage=$page+1;
        }
        return array(
            'page'=>$pag,//分几页
            'model'=>$models,//当前分页的数据
            'backpage'=>$backpage,//返回上一页
            'frontpage'=>$frontpage,//下一页
            'nowpage'=>$page,//当前页
            'limits'=>$limits,//每页的数据条数
        );
    }
}
?>
